package com.loan.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import com.loan.common.annotation.Log;
import com.loan.common.constant.UserConstants;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.Result;
import com.loan.common.core.domain.entity.SysDept;
import com.loan.common.enums.BusinessType;
import com.loan.common.excel.ExcelResult;
import com.loan.common.utils.StringUtils;
import com.loan.common.utils.poi.ExcelUtil;
import com.loan.system.domain.AddressEntity;
import com.loan.system.domain.SysConfig;
import com.loan.system.domain.vo.AddressImportVo;
import com.loan.system.domain.vo.SysUserImportVo;
import com.loan.system.listener.AddressImportListener;
import com.loan.system.listener.SysUserImportListener;
import com.loan.system.service.AddressService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author shiguang on 2022/9/5 10:10
 */
@Validated
@Api(value = "地址管理", tags = {"地址控制器"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/address")
public class AddressController extends BaseController {

    private final AddressService addressService;

    /**
     * 获取部门列表
     */
    @ApiOperation("获取地址列表")
    //@SaCheckPermissio("system:address:list")
    @GetMapping("/list")
    public Result<List<AddressEntity>> list(AddressEntity address) {
        List<AddressEntity> list = addressService.selectAddressList(address);
        return Result.ok(list);
    }

    /**
     * 新增地址
     */
    @ApiOperation("新增地址")
    //@SaCheckPermissio("system:address:add")
    @Log(title = "新增地址", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody AddressEntity entity) {
        if (UserConstants.NOT_UNIQUE.equals(addressService.checkNameUnique(entity))) {
            return Result.fail("新增地址'" + entity.getName() + "'失败，地址已存在");
        }
        return toAjax(addressService.insertAddress(entity));
    }

    /**
     * 根据地址编号获取详细信息
     */
    @ApiOperation("根据地址编号获取详细信息")
    //@SaCheckPermissio("system:address:query")
    @GetMapping(value = "/{id}")
    public Result<AddressEntity> getInfo(@ApiParam("ID") @PathVariable Long id) {
        return Result.ok(addressService.selectAddressById(id));
    }

    /**
     * 获取地址下拉树列表
     */
    @ApiOperation("获取地址下拉树列表")
    @GetMapping("/treeSelect")
    public Result<List<Tree<Long>>> treeSelect(AddressEntity entity) {
        List<AddressEntity> list = addressService.selectAddressList(entity);
        return Result.ok(addressService.buildAddressTreeSelect(list));
    }

    /**
     * 查询地址列表（排除节点）
     */
    @ApiOperation("查询地址列表（排除节点）")
    //@SaCheckPermissio("system:address:list")
    @GetMapping("/list/exclude/{id}")
    public Result<List<AddressEntity>> excludeChild(@ApiParam("地址ID") @PathVariable(value = "id", required = false) Long id) {
        List<AddressEntity> list = addressService.selectAddressList(new AddressEntity());
        list.removeIf(d -> d.getId().equals(id)
                || d.getParentId() != 0);
        return Result.ok(list);
    }

    /**
     * 新增地址
     */
    @ApiOperation("获取所有父类地址")
    //@SaCheckPermissio("system:address:list")
    @GetMapping("parentList")
    public Result<List<AddressEntity>> getParentList() {
        return Result.ok(addressService.getParentList());
    }

    @ApiOperation("导入地址列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
    })
    @Log(title = "地址管理", businessType = BusinessType.IMPORT)
    //@SaCheckPermissio("system:address:import")
    @PostMapping("/importData")
    public Result<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<AddressImportVo> result = ExcelUtil.importExcel(file.getInputStream(), AddressImportVo.class, new AddressImportListener(updateSupport));
        return Result.ok(result.getAnalysis());
    }

    /**
     * 删除地址
     */
    @ApiOperation("删除地址")
    //@SaCheckPermissio("system:address:remove")
    @Log(title = "地址管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result<Void> remove(@ApiParam("地址ID") @PathVariable Long id) {
        return toAjax(addressService.deleteAddressById(id));
    }
}
